import util from './util.js'
var jweixin = require('jweixin-module');  

export default {  
        //判断是否在微信中  
    isWechat:function(){  
        var ua = window.navigator.userAgent.toLowerCase();  
        if(ua.match(/micromessenger/i) == 'micromessenger'){  
            return true;  
        }else{  
            return false;  
        }  
    },  
        //初始化sdk配置  
    initJssdk:function(callback ,url){  
                //服务端进行签名 ，可使用uni.request替换。 签名算法请看文档  
        util.fetchPost('/api/getShareConfig',{url:url},function(res){  
            if(res.data){  
                jweixin.config({  
                    // debug: true,  
                     appId: res.data.appid,  
                     timestamp:res.data.timeStamp,  
                     nonceStr: res.data.nonceStr,  
                     signature:res.data.signature,  
                     jsApiList: [  
                         'checkJsApi',  
                         'onMenuShareTimeline',  
                         'onMenuShareAppMessage'  
                     ]  
                });  
                //配置完成后，再执行分享等功能  
                if(callback){  
                    callback(res.data);  
                }  
            }  

        },function(){},false);  
    },  
        //在需要自定义分享的页面中调用  
    share:function(data ,url){  
	
         url =url ? url :window.location.href;  
		
		
		
		 
		 
        // if(!this.isWechat()){  
        //     return ;  
        // }  
       //每次都需要重新初始化配置，才可以进行分享  
	    url=url.replace("#","?uniapp")
		
		if(uni.getStorageSync('UserInfo')){
			var  userInfo=JSON.parse(uni.getStorageSync('UserInfo'));
			if(userInfo!=null){
				var roteUrl =url.substring(url.indexOf("/pages/"),url.length-1);
				if(roteUrl.indexOf("?")>-1){
					url+="&reffent="+userInfo.userid;
				}else{
					url+="?reffent="+userInfo.userid;
				}
			}
		}
		
		
		
	   //url = url.substring(0,url.indexOf("#"));
	   console.log(url);
        this.initJssdk(function(signData){  
            jweixin.ready(function(){    
                var shareData = {  
                     title: data&&data.title ? data.title : signData.site_name,  
                     desc: data&&data.desc ? data.desc : signData.site_description,  
                     link: url,  
                     imgUrl: data&&data.imgUrl ? data.imgUrl : 'https://lobatu.oss-cn-beijing.aliyuncs.com/data/logo.png',  
                     success: function (res) {  
                                                  
                     },  
                     cancel: function (res) {  
                     }  
                 };  
                 //分享给朋友接口  
                 jweixin.onMenuShareAppMessage(shareData);  
                 //分享到朋友圈接口  
                 jweixin.onMenuShareTimeline(shareData);  
            });  
        } ,window.location.href);  
    }  
}  